// mock/user.ts
const createUserList = () => {
  return [
    {
      userId: 1,
      avatar: 'https://example.com/avatar1.jpg',
      username: 'admin',
      password: '123456',
      token: 'Admin Token',
    },
    {
      userId: 2,
      avatar: 'https://example.com/avatar2.jpg',
      username: 'user',
      password: '123456',
      token: 'User Token',
    },
  ];
};

export default [
  {
    url: '/api/user/login',
    method: 'post',
    response: ({ body }) => {
      const { username, password } = body;
      const user = createUserList().find((item) => item.username === username && item.password === password);
      if (!user) {
        return {
          code: 201,
          message: '账号或密码错误',
        };
      }
      return {
        code: 200,
        data: {
          token: user.token,
        },
      };
    },
  },
  {
    url: '/api/user/info',
    method: 'get',
    response: (request) => {
      const token = request.headers.token;
      const user = createUserList().find((item) => item.token === token);
      if (!user) {
        return {
          code: 201,
          message: '获取用户信息失败',
        };
      }
      return {
        code: 200,
        data: user,
      };
    },
  },
];

//使用的参考示例
//<script setup lang="ts">
// import axios from 'axios';
//
// const fetchData = async () => {
//   try {
//     const response = await axios.post('/api/user/login', { username: 'admin', password: '123456' });
//     console.log(response.data);
//   } catch (error) {
//     console.error('请求出错', error);
//   }
// };
//
// fetchData();
// </script>
